/*
 * Copyright 2009 Al Phillips.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xsd4gorm.xsd.support

import xsd4gorm.model.Domain
import xsd4gorm.model.DomainClass

/**
 *
 * @author Al Phillips
 */
class Resolver {

    public void resolveTypes(Domain domain){

        domain.classes.each {
            it.types.each {type ->
                if(type.getUnresolved()){
                    domain.globalTypes.each {
                        if (type.getType() == it.getName()){
                            // Update the unresolved type to the actual type
                            type.setType(it.getType())
                            type.constraints = it.getConstraints()
//                            type.unresolve()
                        }
                    }
                }
            }
            it.classes.each {cl ->
                cl.types.each {type ->
                    domain.globalTypes.each {
                        if (type.getType() == it.getName()){
                            // Update the unresolved type to the actual type
                            type.setType(it.getType())
                            type.constraints = it.getConstraints()
                            // type.unresolve()
                        }
                    }                    
                }
            } 
        }
    }

    public void resolveClasses(Domain domain){
        // Make a copy to avoid ConcurrentModificationException
        List classes = new ArrayList()
        domain.classes.each {
            // resolveClasses(domain, it)
            it.classes.each {
                classes.add(it)
            }
        }
        classes.each {
            resolveClasses(domain, it)            // domain.addGClass(it)
        }
    }

    public void resolveClasses(Domain domain, DomainClass domainClass){
        domain.addGClass(domainClass)
        domainClass.classes.each {
            resolveClasses(domain, it)
        }
    }

    /*public void resolveOld(Domain domain){
        throw new Exception("I didn't think you needed me anymore...")
        domain.classes.each {
//            println it.getName()
            it.types.each {
//                 println '    ' + it.getName() + ' = ' + it.getType()
                // item is unresolved
                if(it.getUnresolved()){
                    def lookingForName = it.getType()
                    domain.classes.each {
                        if(lookingForName == it.getName()){
                            // todo: now check that it is a class
                            println 'a class?'

                        }
                    }
                }
                        
            }

        }
    }*/

}

